package common.codeGen;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class GenCreateArgument {
	@Autowired
	private GenService genService;
	
	public String create(GenBean genBean){
		StringBuffer sb = new StringBuffer();
		try {
			List<GenColumn> tableNameList = genService.getTableColumnName(genBean.getTableName());
			if(tableNameList==null||tableNameList.size()<=0){
				throw new IllegalArgumentException();
			}
			
			sb.append("package "+genBean.getPackagePath()+".controller;").append("\n");
			sb.append("import yeliang.domain.EntityArgument;\n");
			sb.append("public class "+genBean.getTableName()+"Argument extends EntityArgument{\n");
			sb.append(createPro(tableNameList));
			sb.append(createProMethod(tableNameList));
			
			sb.append("\n");
			sb.append("}").append("\n");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sb.toString();
	}
	
	public String createPro(List<GenColumn> cols){
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<cols.size();i++){
			GenColumn col = cols.get(i);
			if(col.getColumn_name().toLowerCase().endsWith("date")){
				String column_name = col.getColumn_name().substring(0,1).toUpperCase()+col.getColumn_name().substring(1);
				sb.append("private String ").append("start"+column_name).append(";\n");
				sb.append("private String ").append("end"+column_name).append(";\n");
			}else{
				sb.append("private String ").append(col.getColumn_name()).append(";\n");
			}
		}
		return sb.toString();
	}
	
	public String createProMethod(List<GenColumn> cols){
		StringBuffer sb = new StringBuffer();
		for(int i=0;i<cols.size();i++){
			String colName = cols.get(i).getColumn_name();
			String _colName = colName.substring(0,1).toUpperCase()+colName.substring(1);
			if(colName.toLowerCase().endsWith("date")){
				
				sb.append("public String getStart"+_colName+"() {\n");
				sb.append("return start"+_colName+";\n");
				sb.append("}\n");
				
				sb.append("public void setStart"+_colName+"(String start"+_colName+") {\n");
				sb.append("this.start"+_colName+" = start"+_colName+";\n");
				sb.append("}\n");
				
				sb.append("public String getEnd"+_colName+"() {\n");
				sb.append("return end"+_colName+";\n");
				sb.append("}\n");
				
				sb.append("public void setEnd"+_colName+"(String end"+_colName+") {\n");
				sb.append("this.end"+_colName+" = end"+_colName+";\n");
				sb.append("}\n");
				
			}else{
				sb.append("public String get"+_colName+"() {\n");
				sb.append("return "+colName+";\n");
				sb.append("}\n");
				
				sb.append("public void set"+_colName+"(String "+colName+") {\n");
				sb.append("this."+colName+" = "+colName+";\n");
				sb.append("}\n");
			}
			
		}
		return sb.toString();
	}
}
